package com.movitech.mobile.cz.base.bizconfig.controller;


import com.movitech.mobile.cz.base.bizconfig.entity.ext.ExtSSuppliersCategory;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.PageCode;
import com.movitech.mobile.cz.base.sys.bizvo.SuppliersCategoryFindRequest;
import com.movitech.mobile.cz.base.sys.bizvo.SuppliersCategoryRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.bizconfig.entity.SSuppliersCategory;
import com.movitech.mobile.cz.base.bizconfig.service.SSuppliersCategoryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

/**
 * <p>
 * SSuppliersCategory 前端控制器
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@RestController
@RequestMapping("/v1/sSuppliersCategory")
@Api(value = "供应商分类基础操作接口", description = "供应商分类基础操作接口")
public class SSuppliersCategoryController {
    @Autowired
    @Qualifier("sSuppliersCategoryService")
    private SSuppliersCategoryService sSuppliersCategoryService;

    @GetMapping("/allList")
    @ApiOperation(
            value = "获取所有供应商分类接口",
            notes = "获取所有供应商分类接口",
            response = SSuppliersCategory.class
    )
    public RestReturnResult list() {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SSuppliersCategory> page = sSuppliersCategoryService.selectPage(new Page<SSuppliersCategory>());
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @PostMapping("/searchList/{pageNumber}")
    @ApiOperation(
            value = "供应商分类列表查询接口",
            notes = "供应商分类列表查询接口",
            response = SSuppliersCategory.class
    )
    public RestReturnResult searchList(@RequestBody SSuppliersCategory sSuppliersCategory,
                                       @ApiParam(value = "分页页数") @PathVariable Integer pageNumber) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SSuppliersCategory> page = new Page<>();
        page.setCurrent(pageNumber);
        EntityWrapper wrapper = new EntityWrapper<>();
        wrapper.setEntity(sSuppliersCategory);
        page = sSuppliersCategoryService.selectPage(page, wrapper);
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @GetMapping("/getSSuppliersCategory/{id}")
    @ApiOperation(
            value = "供应商分类对象ID获取接口",
            notes = "供应商分类对对象ID获取接口",
            response = SSuppliersCategory.class
    )
    public RestReturnResult addSSuppliersCategory(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        SSuppliersCategory sSuppliersCategory = sSuppliersCategoryService.selectById(id);
        restReturnResult.setData(sSuppliersCategory);
        return restReturnResult;
    }

    @PostMapping("/add")
    @ApiOperation(
            value = "供应商分类添加接口",
            notes = "供应商分类添加接口",
            response = SSuppliersCategory.class
    )
    public RestReturnResult add(@RequestBody SSuppliersCategory sSuppliersCategory) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sSuppliersCategoryService.insert(sSuppliersCategory);
        restReturnResult.setData(sSuppliersCategory);
        return restReturnResult;
    }

    @PutMapping("/update")
    @ApiOperation(
            value = "供应商分类更新接口",
            notes = "供应商分类更新接口",
            response = SSuppliersCategory.class
    )
    public RestReturnResult update(@RequestBody SSuppliersCategory sSuppliersCategory) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sSuppliersCategoryService.updateById(sSuppliersCategory);
        restReturnResult.setData(sSuppliersCategory);
        return restReturnResult;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(
            value = "供应商分类通过ID删除接口",
            notes = "供应商分类通过ID删除接口",
            response = SSuppliersCategory.class
    )
    public RestReturnResult update(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sSuppliersCategoryService.deleteById(id);
        return restReturnResult;
    }

    @GetMapping("/getSSuppliersCategoryByCode/{code}")
    @ApiOperation(
            value = "供应商分类对象编码获取接口",
            notes = "供应商分类对象编码获取接口",
            response = SSuppliersCategory.class
    )
    public RestReturnResult getSSuppliersCategoryByCode(@PathVariable String code) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        List<ExtSSuppliersCategory> categorys = sSuppliersCategoryService.selectByCode(code);
        restReturnResult.setData(categorys);
        return restReturnResult;
    }

    /////////////////////////////////////////////////////

   /* @ApiOperation(
            value = "供应商分类树接口",
            notes = "供应商分类树接口",
            response = SSuppliersCategory.class
    )
    @RequestMapping(value = "/getSuppliersCategoryTree", method = RequestMethod.GET)
    @ResponseBody
    public RestReturnResult<Object> getSuppliersCategoryTree(String level) {
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);

        try {
            hResult = sSuppliersCategoryService.getSuppliersCategoryTreeByLevel(level);
        } catch (Exception e) {
            e.printStackTrace();
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
        }

        return hResult;
    }*/

    @ApiOperation(
            value = "供应商分类树接口",
            notes = "供应商分类树接口",
            response = SSuppliersCategory.class
    )
    @RequestMapping(value = "/public/findSuppliersCategoryTreeList", method = RequestMethod.POST)
    @ResponseBody
    public RestReturnResult<Object> findSuppliersCategoryTreeList(@RequestBody SuppliersCategoryFindRequest suppliersCategoryFindRequest) {
        RestReturnResult<Object> restReturnResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);

        try {
            restReturnResult = sSuppliersCategoryService.findSuppliersCategoryTreeList(suppliersCategoryFindRequest);
        } catch (Exception e) {
            e.printStackTrace();
            restReturnResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
        }

        return restReturnResult;
    }

    /**
     * 供应商分类新增/修改接口
     *
     * @param suppliersCategoryRequestVO
     * @return
     */
    @ApiOperation(
            value = "供应商分类新增/修改接口",
            notes = "供应商分类新增/修改接口",
            response = SSuppliersCategory.class
    )
    @RequestMapping(value = "/saveOrUpdateSuppliersCategory", method = RequestMethod.POST)
    @ResponseBody
    public RestReturnResult<Object> saveOrUpdateSuppliersCategory(@RequestBody SuppliersCategoryRequestVO suppliersCategoryRequestVO) {
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);

        hResult = sSuppliersCategoryService.saveOrUpdateSuppliersCategory(suppliersCategoryRequestVO);

        return hResult;
    }

    /**
     * 供应商删除接口
     *
     * @param id
     * @return
     */
    @ApiOperation(
            value = "供应商删除接口",
            notes = "供应商删除接口",
            response = SSuppliersCategory.class
    )
    @RequestMapping(value = "/deleteSuppliersCategory", method = RequestMethod.GET)
    @ResponseBody
    public RestReturnResult<Object> deleteSuppliersCategory(String id) {
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);

        try {
            hResult = sSuppliersCategoryService.updateSuppliersCategoryForDel(id);
        } catch (Exception e) {
            e.printStackTrace();
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
        }

        return hResult;
    }

    /**
     * 供应商分类 停用/启用 接口
     *
     * @param suppliersCategoryRequestVO
     * @return
     */
    @ApiOperation(
            value = "供应商分类 停用/启用 接口",
            notes = "供应商分类 停用/启用 接口",
            response = SSuppliersCategory.class
    )
    @RequestMapping(value = "/updateSuppliersCategoryStatus", method = RequestMethod.POST)
    @ResponseBody
    public RestReturnResult<Object> updateSuppliersCategoryStatus(@RequestBody SuppliersCategoryRequestVO suppliersCategoryRequestVO) {
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);

        try {
            hResult = sSuppliersCategoryService.updateSuppliersCategoryStatus(suppliersCategoryRequestVO);
        } catch (Exception e) {
            e.printStackTrace();
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
        }

        return hResult;
    }

}

